<?php
class  CommentAction extends Action{
	public function index(){
		$comment=D("Comment");
		$rows=$comment->select();
		dump($rows);
		$this->display();
	}
	//获取商品评价列表
	public function	getComment(){
		
		$Comment=D("Comment");
		//全部数据 
		if (empty($_GET["type"]) or $_GET["type"]==0){
			$where=array(
					"id_value"=>intval($_GET['id']),
					"status"=>1
			);
		}
		//好评
		if ($_GET["type"]==1) {
			$where=getCommentRank($_GET["id"],3,true);
		}
		
		//中评
		if ($_GET["type"]==2) {
			$where=getCommentRank($_GET["id"],2,true);
		}
		//差评
		if ($_GET["type"]==3) {
			$where=getCommentRank($_GET["id"],1,true);
		}
		
		//empty($_GET["number"]) ?10:$number=intval($_GET["number"]);
		//$rows=$Comment->where($where)->order("add_time desc")->limit($number)->select();
		
		//总评价记录
		$count=$Comment->where($where)->count();
		
		
		import("@.Util.Page");
		
		$Page       = new Page($count,empty($_GET["number"])?10:intval($_GET["number"]));
		$Page->gotoPageFun="gotoPages";
		$Page->setConfig('theme',' %linkPage%%downPage% ');
		$show       = $Page->show();
		$rows=$Comment->where($where)->order("add_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign("show",$show);
		
		$this->assign("count",getCommentRank($_GET["id"],0));
		//exit(0);
		//好评、中评、差评
		$praise_count=getCommentRank($_GET["id"],3);
		$medium_count=getCommentRank($_GET["id"],2);
		$poor_count=getCommentRank($_GET["id"],1);
		
		if ($_GET["data_type"]=="html"){
			$this->assign("praise_count",$praise_count);
			$this->assign("medium_count",$medium_count);
			$this->assign("poor_count",$poor_count);
			$this->assign("rows",$rows);
			$this->display();
		}else {
			//混合JSON
			$this->assign("rows",$rows);
			$html=$this->fetch();
			$data["praise_count"]=$praise_count;
			$data["medium_count"]=$medium_count;
			$data["poor_count"]=$poor_count;
			$data["all_count"]=$praise_count+$medium_count+$poor_count;
			$data["html"]=dotran($html);
			JSON($data,"数据获取成功",1);
		}
		
	
	}
	
	//获取好、中、差评的数量 
	public function geteValuationCount(){
		if (empty($_GET["id"])) JSON(0,"请提供商品ID",0);
		
		$data["poor_count"]=getCommentRank($_GET["id"],1);
		$data["medium_count"]=getCommentRank($_GET["id"],2);
		$data["praise_count"]=getCommentRank($_GET["id"],3);
		$data["all_count"]=$data["poor_count"]+$data["medium_count"]+$data["praise_count"];
		JSON($data,"数据获取成功",1);
		
	}
	
	//获取评价回复列表 
	public function getCommentReplyList(){
		
		$jsonData=getCommentReplyList($_GET["pid"],10,false);
		$this->assign("comment_id",intval($_GET["pid"]));
		$i=10;
		foreach ($jsonData as $key=>$val){
			$jsonData[$key]["order_num"]=$i;
			$i--;
		}
		$this->assign("rows",$jsonData);
		if (empty($_GET["pid"])){
			//纯正JSON
			$jsonData=getCommentReplyList($_GET["pid"],5,false);
			JSON($jsonData,"数据获取成功",1);
		}else{
			if ($_GET["data_type"]=="html"){
				$this->display();
			}else{
				//混合JSON
				//dump($jsonData)
				$html=$this->fetch();
				$data["html"]=dotran($html);
				JSON($data,"数据获取成功",1);
			}
			
		}
	}
	//获取评价回复列表全部内容
	public function getCommentReplyAllList(){
		if(empty($_GET["id"])) JSON(0,"没有评价ID",0);
		$comment_reply=D("CommentReply");
		$where=array(
				"comment_id"=>intval($_GET["id"]),
				"status"=>1
				);
		$count=$comment_reply->where($where)->count();
		import("@.Util.Page");
		
		$Page       = new Page($count,empty($_GET["number"])?30:intval($_GET["number"]));
		$Page->gotoPageFun="gotoPage";
		$Page->setConfig('theme',' %linkPage%%downPage% ');
		$show       = $Page->show();
		$rows=$comment_reply->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign("rows",$rows);
		$this->assign("show",$show);
		if (empty($_GET["data_type"])){
			//JSON+HTML
			$html=$this->fetch();
			$data["html"]=dotran($html);
			JSON($data,"数据获取成功",1);
		}elseif ($_GET["data_type"]=="html"){
			//HTML
			$this->display();
		}else{
			//纯JSON
			JSON($rows,"数据获取成功",1);
		}
		
		
	}
	
	
	//添加商品评价,暂时不用
	public function addtComment(){
		if (!check_key()){
			JSON(0,"请登录 后再操作",0);
		}
		exit("测试中...");
		if(empty($_POST["id"])) JSON(0,"没有商品ID",0);
		if (empty($_POST["content"])) JSON(0,"没有内容 ",0);
		$Comment=D("Comment");
		$count=$Comment->where(array("id_value"=>intval($_POST["id"]),"user_id"=>check_key()))->count();
		if ($count>=1){
			JSON(1,"你已经评价过了",1);
		}
		
		$getUserData=getUserData(check_key());
		$data=array(
				"comment_type"=>intval($_POST["comment_type"]),
				"id_value"=>intval($_POST["id"]),
				"email"=>$getUserData["email"],
				"user_name"=>$getUserData["user_name"],
				"content"=>mysql_escape_string($_POST["content"]),
				"comment_rank"=>1,
				"add_time"=>time(),
				"ip_address"=>getIP(),
				"status"=>1,
				"parent_id"=>intval($_POST["parent_id"]),
				"user_id"=>check_key()
				);
		if ($Comment->add($data)){
			if ($_GET["url"]){
				URLS(urldecode($_GET["url"]));
			}
			
			JSON(1,"数据保存成功",1);
		}else{
			
			JSON(0,"数据保存失败",0);
		}
		
		
		
		
	}
	
	//添加商品评价回复 
	public function addCommentReply(){
		if (!check_key()){
			JSON(0,"签名失败",0);
		}
		
		if(empty($_POST["user_id"])) JSON(0,"请登录后再操作",0);
		
		if (empty($_POST["reply_content"])) JSON(0,"请输入回复内容",0);
		
		if (empty($_POST["comment_id"])) JSON(0,"没有评价ID",0);
		
		$comment_reply=D("CommentReply");
		
		$data=array(
				"reply_content"=>$_POST["reply_content"],
				"comment_id"=>$_POST["comment_id"],
				"parent_id"=>0,
				"add_time"=>$_POST["add_time"],
				"user_id"=>$_POST["user_id"],
				"status"=>$_POST["status"]
				
				);
		if ($comment_reply->add($data)){
			JSON(1,"添加成功",1);
		}else{
			JSON(0,"添加失败",0);
		}
		
	}

	//添加商品评价回复的回复
	public function addCommentReplyReply(){
		if (!check_key()){
			JSON(0,"签名失败",0);
		}
		
		if(empty($_POST["user_id"])) JSON(0,"请登录后再操作",0);
		
		if (empty($_POST["parent_id"])) JSON(0,"没有回复ID",0);
		
		if(empty($_POST["reply_content"])) JSON(0,"请输入回复内容",0);
		$comment_reply=D("CommentReply");
		
		//
		$getIdParentData=getIdParentData($_POST["parent_id"]);
		
		$source_user_id=$getIdParentData["user_id"];
		$source_user_name=getUserData($source_user_id,"truename");
		if (empty($source_user_name)) $source_user_name="匿名用户";
		
		
		//$content="@{$source_user_name} ".$_POST["reply_content"];
		//dump($content);
		//exit(0);
		$data=array(
				"reply_content"=>$_POST["reply_content"],
				"parent_id"=>$_POST["parent_id"],
				"add_time"=>$_POST["add_time"],
				"user_id"=>$_POST["user_id"],
				"status"=>$_POST["status"],
				"comment_id"=>$getIdParentData["comment_id"],
		
		);
		if ($comment_reply->add($data)){
			JSON(1,"数据保存成功",1);
			
		}else{
			JSON(0,"数据保存失败",0);
		}
		
	}
	
	public function test(){
		echo "test";
		
	}
}
?>